//
//  RegisterViewController.m
//  OrderMeal
//
//  Created by 宏创 on 14-2-19.
//  Copyright (c) 2014年 周浩. All rights reserved.
//

#import "RegisterViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIView+Toast.h"
#import "UIView+Toast.h"
@interface RegisterViewController ()
{
    NSString *warning;
}
@end

@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(BOOL)checkInput
{
    if ([self.phoneTextField.text length]!=11) {
        warning =@"手机号码必须是11位";
        return NO;
    }
    
    if ([self.lastNameTextField.text length]<2) {
        warning=@"昵称至少2位";
        return NO;
    }    
    if (![self.passwordTextField.text isEqualToString:self.repasswordTextField.text]) {
        warning=@"两次密码输入不一致";
        return NO;
    }
    return YES;
}

-(void)registerWithEmail:(NSString *)email
                lastName:(NSString *)name
                     sex:(NSString *)sex
                   phone:(NSString *)phone
              buildingid:(NSString *)buildingid
                 address:(NSString *)address
              password:(NSString *)password
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    NSDictionary *parameters=@{@"email" : email,
                               @"lastName":name,
                               @"sex":sex,
                               @"phone":phone,
                               @"buildingid":buildingid,
                               @"address" :address,
                               @"password":password

                               };

    [manager GET:register_url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
        [userDefaults setObject:email forKey:@"email"];
        [userDefaults synchronize];
        
        NSNumber *status=responseObject[@"success"];
        
        if ([status isEqualToNumber:@1]) {
            warning =@"注册成功";
            [self.view makeToast:warning duration:2.0 position:@"center"];
            [userDefaults setObject:email forKey:@"username"];
            [userDefaults setObject:password forKey:@"password"];
            [userDefaults synchronize];
            [self dismissViewControllerAnimated:YES completion:nil];
         }else{
           warning =responseObject[@"msg"];
             [self.view makeToast:warning duration:2.0 position:@"center"];
         }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        warning =@"暂时无法登陆";
        [self.view makeToast:warning duration:2.0 position:@"center"];
    }];
}

- (IBAction)doRegister:(UIButton *)sender
{
    if (![self checkInput]) {
        [self.view makeToast:warning duration:2.0 position:@"center"];
    }else{
        [self registerWithEmail:self.emailTextField.text
                       lastName:self.lastNameTextField.text
                            sex:[self.sexTextField.text isEqualToString:@"男"] ? @"1" : @"2"
                          phone:self.phoneTextField.text
                     buildingid:self.buildingidTextField.text
                        address:self.addressTextField.text
                       password:self.passwordTextField.text];

    }
}


#pragma mark- UITextFiekdDelegate

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
